<template>
  <section
    v-if="menu"
    :class="{'nb-dropdown': optionsList.length}"
    :style="{backgroundColor: colorMap[type]}"
  >
    <div
      class="before-btn"
      v-if="optionsList.length"
      @click="clickMenu(optionsList[0].value)"
      :class="{pr15: optionsList.length > 1}"
    >
      <span v-if="optionsList[0].icon" :class="optionsList[0].icon"></span>
      {{optionsList[0].label}}
    </div>
    <el-dropdown
      @command="handleCommand"
      v-if="optionsList.length > 1"
    >
      <span  class="el-dropdown-link">
        <i class="el-icon-arrow-down el-icon--right"></i>
      </span>
      <el-dropdown-menu slot="dropdown">
        <el-dropdown-item
          v-for="(item, index) in optionsList"
          v-if="index > 0"
          :command="item.value"
          :key="item.value"
        >{{item.label}}</el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>
  </section>
  <section
    v-else
    class="nb-button-wrapper"
    :style="{backgroundColor: colorMap[type]}"
    @click="handleClick"
    :class="{text: text, large: large, plain: plain}"
  >
    <span v-if="icon" :class="icon" style="margin-right: 6px;"></span>
    <slot></slot>
  </section>
</template>

<script>
  export default {
    name: 'NbButton',
    data () {
      return {
        colorMap: {
          primary: '#3497ff',
          success: 'rgb(13, 202, 153)',
          plain: '#d9d9d9'
        }
      }
    },
    props: {
      label: {
        type: String,
        default: ''
      },
      type: {
        type: String,
        default: 'primary'
      },
      text: Boolean,
      icon: String,
      width: {
        type: String,
        default: '71'
      },
      menu: Boolean,
      options: {
        type: Array,
        default: () => {
          return []
        }
      },
      large: Boolean,
      plain: Boolean
    },
    computed: {
      optionsList () {
        return this.options.filter(item => {
          return !item.disabled
        })
      }
    },
    methods: {
      handleClick (evt) {
        this.$emit('click', evt)
      },
      clickButton () {
      },
      handleCommand (command) {
        this.$emit('click', command)
      },
      clickMenu (command) {
        this.$emit('click', command)
      }
    },
    mounted () {
    }
  }
</script>

<style lang="scss">
.nb-button-wrapper{
  display: inline-block;
  border-radius: 2px;
  cursor: pointer;
  text-align: center;
  font-size: 13px;
  color: white;
  height: 32px;
  line-height: 32px;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  padding: 0 10px;
  margin-left: 10px;
}
  .nb-button-wrapper.text{
    background-color: white !important;
    border: 1px solid #d9d9d9 !important;
    color: #999;
  }
  .nb-button-wrapper.large{
    min-width: 96px;
  }
  .nb-button-wrapper.plain{
    backgroundColor: #d9d9d9 !important;
  }
  .nb-dropdown{
    display: inline-block;
    height: 32px;
    line-height: 32px;
    background: rgb(13, 202, 153);
    color: white;
    border-radius: 2px;
    padding: 0 10px 0 10px;
    font-size: 0;
    vertical-align: top;
    margin-left: 10px;
    .before-btn{
      display: inline-block;
      font-size: 13px;
      margin-right: 5px;
      height: 31px;
      cursor: pointer;
      span{
        color: white;
        font-weight: 900 !important;
        margin-right: 5px;
      }
    }
    .before-btn.pr15{
      border-right: 1px solid rgba(255,255,255,.4);
      padding-right: 10px;
    }
    .el-dropdown{
      i{
        color: white;
        cursor: pointer;
        font-weight: 900;
      }
    }
  }
</style>
